package com.code.google.mdsc.io.xml.impl.factory;

import java.math.BigDecimal;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import com.code.google.mdsc.model.Booking;
import com.code.google.mdsc.sepa.GroupHeaderSDD;
import com.code.google.mdsc.sepa.PartyIdentificationSEPA1;

/**
 * Generates the GroupHeader XML part.
 * 
 */
public class GrpHdrFactory extends PartialSEPAFactory {

	/**
	 * Generates the {@link GroupHeaderSDD} with specified data. Encapsulates
	 * the data, which is equal for all payment instructions.
	 * 
	 * @param bookingData
	 *            the bookingData
	 * @param cashSum
	 *            the cash amount which is specified in the
	 *            {@link PaymentInstructionInformationSDD}s. It must not be
	 *            <code>null</code>.
	 * 
	 * @return the {@link GroupHeaderSDD} with the specified data.
	 */

	public GroupHeaderSDD generate(Booking bookingData, BigDecimal cashSum) {
		GroupHeaderSDD generated = OBJECT_FACTORY.createGroupHeaderSDD();

		// Die <MsgID> in Kombination mit der Kunden-ID oder der
		// Auftrag-geber-IBAN kann als Kriterium fuer die Verhinderung einer
		// Doppelver-arbeitung bei versehentlich doppelt einge-reichten Dateien
		// dienen und muss somit fuer jede neue pain-Nachricht einen neuen Wert
		// ent-halten.
		generated.setMsgId("" + System.currentTimeMillis());

		GregorianCalendar credttmDate = new GregorianCalendar();

		credttmDate.setTimeZone(TimeZone.getTimeZone("CET"));
		generated.setCreDtTm(datatypeFactory
				.newXMLGregorianCalendar(credttmDate));

		generated.setNbOfTxs("" + bookingData.getDebtors().size()); // TODO max
																	// 15?
		generated.setCtrlSum(cashSum);

		// generateUltimateCreditor(); maybe not necessary for our
		// purpose?

		PartyIdentificationSEPA1 pisep1 = OBJECT_FACTORY
				.createPartyIdentificationSEPA1();

		pisep1.setNm(bookingData.getCreditor().getName());
		// OrganisationIdentificationSEPAChoice oidentsepaChoice =
		// OBJECT_FACTORY
		// .createOrganisationIdentificationSEPAChoice();

		// oidentsepaChoice.setBICOrBEI("BIC");
		//
		// GenericOrganisationIdentification1 genericOrganisationIdentification1
		// = OBJECT_FACTORY
		// .createGenericOrganisationIdentification1();
		//
		// genericOrganisationIdentification1.setId("Id");
		// genericOrganisationIdentification1.setIssr("Issr");
		// OrganisationIdentificationSchemeName1Choice
		// organisationIdentificationSchemeName1Choice = OBJECT_FACTORY
		// .createOrganisationIdentificationSchemeName1Choice();
		//
		// organisationIdentificationSchemeName1Choice.setCd("Cd"); //
		// organisationIdentificationSchemeName1Choice.setPrtry("Prtry");
		// genericOrganisationIdentification1
		// .setSchmeNm(organisationIdentificationSchemeName1Choice);
		// oidentsepaChoice.setOthr(genericOrganisationIdentification1);

		// PartySEPAChoice psepaChoice = OBJECT_FACTORY.createPartySEPAChoice();
		// psepaChoice.setOrgId(oidentsepaChoice);
		// pisep1.setId(psepaChoice);
		generated.setInitgPty(pisep1);

		return generated;
	}

	@SuppressWarnings("unused")
	private PartyIdentificationSEPA1 generateUltimateCreditor() {
		PartyIdentificationSEPA1 pisepa1 = OBJECT_FACTORY
				.createPartyIdentificationSEPA1();
		// pisepa1.setId(null) do not set this, page 76
		pisepa1.setNm("abweichender Zahlungsempfaenger");

		return pisepa1;
	}

}
